package com.manage.common.core.result;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.manage.common.core.enums.CommonEnum;
import lombok.Data;

/**
 * @author zhangqing
 * @version V1.0
 * @description 统一返回对象
 * @date 7/12/2020 6:32 PM
 */
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class JsonResult {
    private Integer code;
    private String message;
    private Object data;

    public JsonResult(Integer code, String message, Object data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public JsonResult(String message) {
        this.message = message;
    }

    public JsonResult(Object data) {
        this.data = data;
    }

    public JsonResult(String message, Object data) {
        this.message = message;
        this.data = data;
    }

    public JsonResult(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public JsonResult() {
    }

    /**
     * 响应返回结果
     *
     * @param rows 影响行数
     * @return 操作结果
     */
    public static JsonResult toJsonResult(int rows) {
        return rows > 0 ? success() : failure();
    }

    /**
     * 响应返回结果
     *
     * @param result 结果
     * @return 操作结果
     */
    public static JsonResult toJsonResult(boolean result) {
        return result ? success() : failure();
    }

    public static JsonResult success() {
        return new JsonResult(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getMessage());
    }

    public static JsonResult success(String message) {
        return new JsonResult(CommonEnum.SUCCESS.getCode(), message);
    }

    public static JsonResult success(Object data) {
        return new JsonResult(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getMessage(), data);
    }

    public static JsonResult success(String message, Object data) {
        return new JsonResult(CommonEnum.SUCCESS.getCode(), message, data);
    }

    public static JsonResult success(int status, String message, Object data) {
        return new JsonResult(status, message, data);
    }

    public static JsonResult failure() {
        return new JsonResult(CommonEnum.FAIL.getCode(), CommonEnum.FAIL.getMessage());
    }

    public static JsonResult failure(String message) {
        return new JsonResult(CommonEnum.FAIL.getCode(), message);
    }

    public static JsonResult failure(int status, String message) {
        return new JsonResult(status, message);
    }

    public static JsonResult failure(int status, String message, Object data) {
        return new JsonResult(status, message, data);
    }
}
